package cn.store.web.servlet;

import cn.store.service.ProductService;
import cn.store.service.serviceImp.ProductServiceImp;
import cn.store.service.serviceImp.UserServiceImp;
import cn.store.domain.User;
import cn.store.service.UserService;
import cn.store.utils.MyBeanUtils;
import cn.store.utils.UUIDUtils;
import cn.store.utils.UserPrivCode;
import cn.store.web.base.BaseServlet;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class UserServlet extends BaseServlet {

	public String registUI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		return "/jsp/register.jsp";
	}
	
	public String loginUI(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return "/jsp/login.jsp";
	}

	public String userRegistcheck(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 接收表单参数
		String  username = request.getParameter("name");

		Map<String,String> msg = new HashMap<>();
		response.setContentType("application/json; charset=utf-8");
		PrintWriter p =response.getWriter();
		UserService UserService=new UserServiceImp();
		User user03=UserService.userRegistcheck(username);
		System.out.println(user03);


		try {
			/*尝试删除*/
			if(user03==null) {

				msg.put("flag", "true");
			}
			else
			{
				msg.put("flag","false");
			}
		}catch (Exception e){
			/*删除失败*/;
		}
		finally {
			String jsonStr = JSON.toJSONString(msg);
			System.out.println(jsonStr);
			p.write(jsonStr);
			return null;
		}

//		UserService UserService=new UserServiceImp();
//		if(UserService.userRegistcheck(username)==null) {
//			request.setAttribute("msg", "用户名可用!");
//			return "/jsp/register.jsp";
//		}
//		else
//		{
//			request.setAttribute("msg", "用户名已存在!");
//			return "/jsp/info.jsp";
//		}
	}

	// userRegist
	public String userRegist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 接收表单参数
		Map<String, String[]> map = request.getParameterMap();
		User user = new User();
		MyBeanUtils.populate(user, map);
		// 为用户的其他属性赋值
		UserService UserService=new UserServiceImp();
		//注册验证用户名
//		String name=user.getUsername();
//
//		User user03=UserService.userRegistcheck(user);
//		if(user03!=null)
//		{
//
//		}
		//select * from user where username=?  and password=?


		user.setUid(UUIDUtils.getId());
		user.setState(UserPrivCode.USER);//设置成普通用户
		user.setCode(UUIDUtils.getCode());
		System.out.println(user);

		/*
		 * //遍历map中的数据 Set<String> keySet = map.keySet(); Iterator<String>
		 * iterator = keySet.iterator(); while(iterator.hasNext()){ String
		 * str=iterator.next(); System.out.println(str); String[] strs =
		 * map.get(str); for (String string : strs) {
		 * System.out.println(string); } System.out.println(); }
		 */

		// 调用业务层注册功能
		try {
			UserService.userRegist(user);
			request.setAttribute("msg", "用户注册成功!请登录!");

		} catch (Exception e) {
			// 注册失败,跳转到提示页面
			System.out.println(e);
			request.setAttribute("msg", "用户注册失败,请重新注册!");

		}
		return "/jsp/info.jsp";
	}
	
	//userLogin
	public String userLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获取用户数据(账户/密码)
		User user=new User();
		MyBeanUtils.populate(user, request.getParameterMap());
		
		//调用业务层登录功能
		UserService UserService=new UserServiceImp();
		User user02=null;
		try {
			//select * from user where username=?  and password=?
			user02=UserService.userLogin(user);
			//用户登录成功,将用户信息放入session中
			request.getSession().setAttribute("loginUser", user02);
			response.sendRedirect(request.getContextPath()+"/index.jsp");
			return null;
		} catch (Exception e) {
			//用户登录失败
			String msg=e.getMessage();
			System.out.println(msg);
			//向request放入失败的信息
			request.setAttribute("msg", msg);
			return "/jsp/login.jsp";
		}
	}
	
	public String logOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
		  //清除session
			request.getSession().invalidate();
		  //重新定向到首页
		  response.sendRedirect(request.getContextPath()+"/index.jsp");
		  return null;	
	}
	public String logoutAdmin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//清除session
		request.getSession().invalidate();
		return null;
	}
}
